#include "fig_enemy_3drotate.h"

FIGEnemy3DRotate::FIGEnemy3DRotate(ENEMY_TYPES EnemyType) :
															FIGEnemyCraft(EnemyType),
															m_IsUFORotate(false),
															m_RotationUFO(0)
{

}

FIGEnemy3DRotate::ShipSpriteInfo::ShipSpriteInfo() : 
										m_ShipIndex(FIGEnemy3DRotate::ShipSpriteInfo::CRAFT_MIDDLE),
										m_PositionCounter(0),
										m_RotationPrevious(0)										
{
	
}

void	FIGEnemy3DRotate::Load(const std::string& Name)
{
	char	Buffer[256];

	if( Name.compare("enemy_ufo_1") == 0 || 
		Name.compare("enemy_ufo_2") == 0
		)
	{
		m_IsUFORotate = true;
	}
	else 
	{
		m_IsUFORotate = false;
	}


	for(int i = 0; i < 11; i++)
	{
		sprintf(Buffer, "%s_%04d", Name.c_str(), i );
		m_ShipSpriteInfo.AddSprite(Buffer);
	}

	m_Radius = m_ShipSpriteInfo.m_Sprites[ShipSpriteInfo::CRAFT_MIDDLE]->m_SizeOriginal.x * 0.4f;
	
}

void	FIGEnemy3DRotate::ShipSpriteInfo::Reset(float Rotatoin)
{
	m_ShipIndex = FIGEnemy3DRotate::ShipSpriteInfo::CRAFT_MIDDLE;
	m_PositionCounter = 0;
	m_RotationPrevious = Rotatoin;
}



void	FIGEnemy3DRotate::ShipSpriteInfo::Tick(float dt ,											
											float Rotation
											)
{
	
	const float POSITION_COUNTER_DURATION = 0.1f;
	// LEFT , RIGHT , MIDDLE determination
	if(m_PositionCounter <= 0)
	{
		const float MIN_ROTATION = 1.0f;
		float Diff = Rotation - m_RotationPrevious;		
		
		if( Diff > MIN_ROTATION)
		{
			// goinig left
			GoingLeft();
		}
		else if ( Diff < -MIN_ROTATION)
		{
			// going right
			GoingRight();
		}
		else
		{
			// going mid
			GoingMid();
		}
        
		m_PositionCounter = POSITION_COUNTER_DURATION;
		m_RotationPrevious = Rotation;
    }
	else
	{
		m_PositionCounter -= dt;
	}

	m_Sprites[m_ShipIndex]->Tick(dt);

	
	
}

void	FIGEnemy3DRotate::ShipSpriteInfo::GoingLeft()
{
	if( m_ShipIndex <= 0)
		return;
	
	int	Temp = (int)m_ShipIndex;
	Temp--;
	m_ShipIndex = (FIGEnemy3DRotate::ShipSpriteInfo::CRAFT_SPRITES)Temp;
}

void	FIGEnemy3DRotate::ShipSpriteInfo::GoingRight()
{
	if( m_ShipIndex >= CRAFT_SPRITE_COUNT-1)
		return;
    
	int	Temp = (int)m_ShipIndex;
	Temp++;
	m_ShipIndex = (FIGEnemy3DRotate::ShipSpriteInfo::CRAFT_SPRITES)Temp;
}

void	FIGEnemy3DRotate::ShipSpriteInfo::GoingMid()
{
	int	Temp = (int)m_ShipIndex;	
	
	if(m_ShipIndex < CRAFT_MIDDLE)
		Temp++;	
	else if(m_ShipIndex > CRAFT_MIDDLE)
		Temp--;

	m_ShipIndex = (FIGEnemy3DRotate::ShipSpriteInfo::CRAFT_SPRITES)Temp;
    
	//m_ShipIndex = CRAFT_MIDDLE;
}

void	FIGEnemy3DRotate::ShipSpriteInfo::AddSprite(const std::string& Str)
{
	ui::UISprite* Spr = new ui::UISprite();
	Spr->Create(Str);
	m_Sprites.push_back(Spr);
	
}

FIGEnemy3DRotate::ShipSpriteInfo::~ShipSpriteInfo()
{
	for(int i = 0; i < (int)m_Sprites.size(); i++)
	{
		UTIL_SAFE_DELETE(m_Sprites[i]);
	}

	m_Sprites.resize(0);
}


void	FIGEnemy3DRotate::Render(const math::Vec2& Offset, float Scale)
{
	m_pSprite = m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.m_ShipIndex];

	FIGEnemyCraft::Render(Offset, Scale);
}

void	FIGEnemy3DRotate::Tick(float dt)
{	
	FIGEnemyCraft::Tick(dt);

	if(!m_IsAlive)
		return;

	if(m_DelaySpawn > 0)
		return;

	m_ShipSpriteInfo.Tick(dt, m_Rotation);
	

	if(m_IsUFORotate)
	{
		m_RotationUFO += ( 180.0f * dt);		
	}

	if( m_RotationUFO > 360.0f)
		m_RotationUFO -= 360.0f;

	m_Size3DRotateEnmey = m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.m_ShipIndex]->m_SizeOriginal;
	m_Radius = m_Size3DRotateEnmey.x * 0.5f;
}


